package weaver.interfaces.cese.duss.action.srm;

import com.engine.cese.duss.srm.util.GenUtil;
import com.weaver.general.BaseBean;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-23
 * @Description: 采购计划更新需求池当前阶段，剩余招标周期，偏差时间，偏差状态
 * @ModifyLog:
 **/
/*
 *@Description: 采购计划更新需求池当前阶段，剩余招标周期，偏差时间，偏差状态
 *@Param:
 *@return:
 *@Author: Dusongsong
 *@Date: 2021/08/18
 */
public class WeaPurchasePlanAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        try {
            switch (tablename) {
                case "formtable_main_1033"://招标采购立项
                    rs.executeQuery( "select m.dbjcsprq,n.id,n.xqrq from uf_cgxq_wl n left join(select a.id,a.xqdid,b.requestid,b.dbjcsprq\n" +
                            "from uf_cgrw_wl a,formtable_main_1033 b where EXISTS (SELECT COLUMN_VALUE\n" +
                            "FROM TABLE(SPLITSTR(b.rwdids, ',')) c WHERE c.column_value = a.id))m\n" +
                            "on n.id=m.xqdid where m.requestid=?", requestid );
                    if (rs.next()) {
                        String dbjcsprq = Util.null2String( rs.getString( "dbjcsprq" ) );
                        long syzbzq = getDays( dbjcsprq );
                        String xqrq = Util.null2String( rs.getString( "xqrq" ) );
                        String id = Util.null2String( rs.getString( "id" ) );
                        long pcrq = getDays( xqrq ) + 4;
                        if (syzbzq <= 0) {
                            rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=1,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                        } else if (syzbzq <= 7) {
                            rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=1,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                        } else if (syzbzq > 7) {
                            rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=1,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                        }
                    }

                    break;
                case "formtable_main_1038"://供应商资格预审
                    String sql = "select cglxd from " + tablename + " where requestid=?";
                    rs.executeQuery( sql, requestid );
                    if (rs.next()) {
                        String cglxd = Util.null2String( rs.getString( "cglxd" ) );
                        rs.executeQuery( "select m.zgshwcrq,m.dbjcsprq,n.id from uf_cgxq_wl n left join(select a.id,a.xqdid,b.requestid,b.zgshwcrq,b.dbjcsprq\n" +
                                "from uf_cgrw_wl a,formtable_main_1033 b where EXISTS (SELECT COLUMN_VALUE\n" +
                                "FROM TABLE(SPLITSTR(b.rwdids, ',')) c WHERE c.column_value = a.id))m\n" +
                                "on n.id=m.xqdid where m.requestid=?", cglxd );
                        while (rs.next()) {
                            String dbjcsprq = Util.null2String( rs.getString( "dbjcsprq" ) );
                            String zgshwcrq = Util.null2String( rs.getString( "zgshwcrq" ) );
                            String id = Util.null2String( rs.getString( "id" ) );
                            long syzbzq = getDays( dbjcsprq );
                            long pcrq = getDays( zgshwcrq );
                            if (syzbzq <= 0) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=2,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq <= 7) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=2,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq > 7) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=2,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            }
                        }
                    }
                    break;
                case "formtable_main_1042"://发起招标
                    String sql1 = "select cglxd from " + tablename + " where requestid=?";
                    rs.executeQuery( sql1, requestid );
                    if (rs.next()) {
                        String cglxd = Util.null2String( rs.getString( "cglxd" ) );
                        rs.executeQuery( "select m.dbjcsprq,m.zbwjfbrq,n.id from uf_cgxq_wl n left join(select a.id,a.xqdid,b.requestid,b.zbwjfbrq,b.dbjcsprq\n" +
                                "from uf_cgrw_wl a,formtable_main_1033 b where EXISTS (SELECT COLUMN_VALUE\n" +
                                "FROM TABLE(SPLITSTR(b.rwdids, ',')) c WHERE c.column_value = a.id))m\n" +
                                "on n.id=m.xqdid where m.requestid=?", cglxd );
                        while (rs.next()) {
                            String dbjcsprq = Util.null2String( rs.getString( "dbjcsprq" ) );
                            String zbwjfbrq = Util.null2String( rs.getString( "zbwjfbrq" ) );
                            String id = Util.null2String( rs.getString( "id" ) );
                            long syzbzq = getDays( dbjcsprq );
                            long pcrq = getDays( zbwjfbrq );
                            if (syzbzq <= 0) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=3,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq <= 7) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=3,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq > 7) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=3,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            }
                        }
                    }
                    break;
                case "formtable_main_1043"://供应商投标
                    String sql2 = "select cglxd from " + tablename + " where requestid=?";
                    rs.executeQuery( sql2, requestid );
                    if (rs.next()) {
                        String cglxd = Util.null2String( rs.getString( "cglxd" ) );
                        rs.executeQuery( "select m.tbjzrq,m.dbjcsprq,n.id from uf_cgxq_wl n left join(select a.id,a.xqdid,b.requestid,b.tbjzrq,b.dbjcsprq\n" +
                                "from uf_cgrw_wl a,formtable_main_1033 b where EXISTS (SELECT COLUMN_VALUE\n" +
                                "FROM TABLE(SPLITSTR(b.rwdids, ',')) c WHERE c.column_value = a.id))m\n" +
                                "on n.id=m.xqdid where m.requestid=?", cglxd );
                        while (rs.next()) {
                            String dbjcsprq = Util.null2String( rs.getString( "dbjcsprq" ) );
                            String tbjzrq = Util.null2String( rs.getString( "tbjzrq" ) );
                            String id = Util.null2String( rs.getString( "id" ) );
                            long syzbzq = getDays( dbjcsprq );
                            long pcrq = getDays( tbjzrq );
                            if (syzbzq <= 0) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=4,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq <= 5) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=4,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq > 5) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=4,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            }
                        }
                    }
                    break;
                case "formtable_main_1046"://开标结果会签
                    String sql3 = "select cglxd from " + tablename + " where requestid=?";
                    rs.executeQuery( sql3, requestid );
                    if (rs.next()) {
                        String cglxd = Util.null2String( rs.getString( "cglxd" ) );
                        rs.executeQuery( "select m.slkbrq,m.dbjcsprq,n.id from uf_cgxq_wl n left join(select a.id,a.xqdid,b.requestid,b.slkbrq,b.dbjcsprq\n" +
                                "from uf_cgrw_wl a,formtable_main_1033 b where EXISTS (SELECT COLUMN_VALUE\n" +
                                "FROM TABLE(SPLITSTR(b.rwdids, ',')) c WHERE c.column_value = a.id))m\n" +
                                "on n.id=m.xqdid where m.requestid=?", cglxd );
                        while (rs.next()) {
                            String dbjcsprq = Util.null2String( rs.getString( "dbjcsprq" ) );
                            String slkbrq = Util.null2String( rs.getString( "slkbrq" ) );
                            String id = Util.null2String( rs.getString( "id" ) );
                            long syzbzq = getDays( dbjcsprq );
                            long pcrq = getDays( slkbrq );
                            if (syzbzq <= 0) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=5,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq <= 5) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=5,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq > 5) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=5,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            }
                        }
                    }
                    break;
                case "formtable_main_1049"://评标结果会签
                    String sql4 = "select cglxd from " + tablename + " where requestid=?";
                    rs.executeQuery( sql4, requestid );
                    if (rs.next()) {
                        String cglxd = Util.null2String( rs.getString( "cglxd" ) );
                        rs.executeQuery( "select m.pbjghqrq,m.dbjcsprq,n.id from uf_cgxq_wl n left join(select a.id,a.xqdid,b.requestid,b.pbjghqrq,b.dbjcsprq\n" +
                                "from uf_cgrw_wl a,formtable_main_1033 b where EXISTS (SELECT COLUMN_VALUE\n" +
                                "FROM TABLE(SPLITSTR(b.rwdids, ',')) c WHERE c.column_value = a.id))m\n" +
                                "on n.id=m.xqdid where m.requestid=?", cglxd );
                        while (rs.next()) {
                            String dbjcsprq = Util.null2String( rs.getString( "dbjcsprq" ) );
                            String pbjghqrq = Util.null2String( rs.getString( "pbjghqrq" ) );
                            String id = Util.null2String( rs.getString( "id" ) );
                            long syzbzq = getDays( dbjcsprq );
                            long pcrq = getDays( pbjghqrq );
                            if (syzbzq <= 0) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=6,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq <= 2) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=6,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq > 2) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=6,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            }
                        }
                    }
                    break;
                case "formtable_main_1050"://供应商发起决策
                    String sql5 = "select cglxd from " + tablename + " where requestid=?";
                    rs.executeQuery( sql5, requestid );
                    if (rs.next()) {
                        String cglxd = Util.null2String( rs.getString( "cglxd" ) );
                        rs.executeQuery( "select m.dbjcsprq,n.id from uf_cgxq_wl n left join(select a.id,a.xqdid,b.requestid,b.dbjcsprq\n" +
                                "from uf_cgrw_wl a,formtable_main_1033 b where EXISTS (SELECT COLUMN_VALUE\n" +
                                "FROM TABLE(SPLITSTR(b.rwdids, ',')) c WHERE c.column_value = a.id))m\n" +
                                "on n.id=m.xqdid where m.requestid=?", cglxd );
                        while (rs.next()) {
                            String dbjcsprq = Util.null2String( rs.getString( "dbjcsprq" ) );
                            String id = Util.null2String( rs.getString( "id" ) );
                            long syzbzq = getDays( dbjcsprq );
                            if (syzbzq <= 0) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=7,t.pcsj=? where t.id=?", syzbzq, syzbzq, id );
                            } else if (syzbzq <= 2) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=7,t.pcsj=? where t.id=?", syzbzq, syzbzq, id );
                            } else if (syzbzq > 2) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=7,t.pcsj=? where t.id=?", syzbzq, syzbzq, id );
                            }
                        }
                    }
                    break;
                case "formtable_main_1052"://中标通知
                    String sql6 = "select cglxd from " + tablename + " where requestid=?";
                    rs.executeQuery( sql6, requestid );
                    if (rs.next()) {
                        String cglxd = Util.null2String( rs.getString( "cglxd" ) );
                        rs.executeQuery( "select m.zbtzrq,m.dbjcsprq,n.id from uf_cgxq_wl n left join(select a.id,a.xqdid,b.requestid,b.zbtzrq,b.dbjcsprq\n" +
                                "from uf_cgrw_wl a,formtable_main_1033 b where EXISTS (SELECT COLUMN_VALUE\n" +
                                "FROM TABLE(SPLITSTR(b.rwdids, ',')) c WHERE c.column_value = a.id))m\n" +
                                "on n.id=m.xqdid where m.requestid=?", cglxd );
                        while (rs.next()) {
                            String dbjcsprq = Util.null2String( rs.getString( "dbjcsprq" ) );
                            String zbtzrq = Util.null2String( rs.getString( "zbtzrq" ) );
                            String id = Util.null2String( rs.getString( "id" ) );
                            long syzbzq = getDays( dbjcsprq );
                            long pcrq = getDays( zbtzrq );
                            if (syzbzq <= 0) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=0,t.syzbzq=?,t.dqjd=8,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq <= 7) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=1,t.syzbzq=?,t.dqjd=8,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            } else if (syzbzq > 7) {
                                rs.executeUpdate( "update uf_cgxq_wl t set t.pczt=2,t.syzbzq=?,t.dqjd=8,t.pcsj=? where t.id=?", syzbzq, pcrq, id );
                            }
                        }
                    }
                    break;

            }

            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    public static long getDays(String date) {
        long days = 0L;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
            Date d1 = sdf.parse( date );
            long ts1 = d1.getTime();
            Date d2 = sdf.parse( DateUtil.getCurrentDate() );
            long ts2 = d2.getTime();
            days = (ts1 - ts2) / (1000 * 60 * 60 * 24);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }
}
